package com.sf.sgs.sessionevaluate.controller;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sf.sgs.sessionevaluate.controller.dto.req.SessionAnswerReq;
import com.sf.sgs.sessionevaluate.controller.response.RestResponse;
import com.sf.sgs.sessionevaluate.domain.SessionAnswer;
import com.sf.sgs.sessionevaluate.domain.SessionInfo;
import com.sf.sgs.sessionevaluate.service.ISessionAnswerService;

@Controller
@RequestMapping("sessionAnswers")
public class SessionAnswerController extends BaseController {

	@Autowired
	private ISessionAnswerService answerService;

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public RestResponse<?> get(@PathVariable(value = "id") Long id) {
		return handle(new Action<SessionAnswer>() {

			@Override
			public void onHandle(RestResponse<SessionAnswer> restResponse) {
				SessionAnswer sessionAnswer = answerService.getById(id);
				System.out.println(sessionAnswer.getAnswerA1());
				restResponse.setResult(sessionAnswer);
			}
		});
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public RestResponse<?> save(@RequestBody SessionAnswerReq sessionAnswerReq) {
		return handle(new Action<SessionAnswer>() {

			@Override
			public void onHandle(RestResponse<SessionAnswer> restResponse) {
				SessionAnswer sessionAnswer = new SessionAnswer();
				BeanUtils.copyProperties(sessionAnswerReq, sessionAnswer, "gainFeedback");
				SessionInfo sessionInfo = new SessionInfo();
				sessionInfo.setId(sessionAnswerReq.getSessionId());
				sessionAnswer.setSessionInfo(sessionInfo);
				String joinStr = String.join(",", sessionAnswerReq.getGainFeedback());
				sessionAnswer.setGainFeedback(joinStr);
				sessionAnswer.setCreateTime(new Date());
				answerService.save(sessionAnswer);
				restResponse.setResult(sessionAnswer);
			}
		});
	}

	@RequestMapping(path = "/success")
	public String addSession() {
		return "answerSuccess";
	}
}
